

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.KeyEventDispatcher;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Draw extends JPanel implements ActionListener, KeyListener, MouseListener {
	
	private RoadMap[][] map;
	private int[][] map_point;
	private Map m;
	
//	private int prev_x = -9999;
//	private int prev_y = -9999;
//	private int prev_area = -9999;
	
	public Timer timer;
	
	public Draw (Map __parent, RoadMap[][] __map) {
		addKeyListener(this);
		addMouseListener(this);
		setRequestFocusEnabled(true);
		setFocusable(true);
				
		this.m = __parent;
		this.map = __map;

		int length = 0;

		for (int i=0; i<this.map.length; i++) {			
			length += this.map[i].length;
		}
		
		map_point = new int[length][6];
		
	    //timer = new Timer(50, this);
	    //timer.setInitialDelay(190);
	    //timer.start();
	}

	public void get (RoadMap[][] __map) {
		this.map = __map;
	}
	
	public void set (Graphics g) {
        int w = getWidth() / map.length;
        int h = getHeight() / map[0].length;
//        int h = getHeight() / map[0].length;
//		int w = h;
		int length = 0;
		BufferedImage image;
        
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
        		image = map[i][j].getImage();
        		
        		int start_x = i*w;
        		int start_y = j*h;
        		int last_x = i*w + w;
        		int last_y = j*h + h;
        		
        		if (image != null) {
					g.drawImage(map[i][j].getImage(), start_x, start_y, w, h, null);
        		}
        		else {
                	g.setColor(map[i][j].getColor());
                    g.fillRect(start_x, start_y, w, h);
        		}
        		
        		// start_x, start_y, last_x, last_y
        		//
        		map_point[length][0] = start_x;
        		map_point[length][1] = start_y;
        		map_point[length][2] = last_x;
        		map_point[length][3] = last_y;
        		map_point[length][4] = i;
        		map_point[length][5] = j;
        		length++;
            }
        }
	}
	
	public void setSize (int x, int y, int width, int height) {
		setBounds(x, y, width, height);
	}
	
//	public void change () {
//		int x = (int) (Math.random() * map.length);
//		int y = (int) (Math.random() * map[0].length);
//		
//		if (!(this.prev_x == -9999 || prev_y == -9999 || prev_area == -9999)) {			
//			this.map[this.prev_x][this.prev_y] = new RoadMap(this.prev_area);
//		}
//		
//		this.prev_x = x;
//		this.prev_y = y;
//		this.prev_area = this.map[x][y].getChildId();
//		
//		this.map[x][y] = new RoadMap(-1);
//	}
	
	public void paint(Graphics g) {
	    super.paintComponent(g);
	    Graphics2D g2 = (Graphics2D) g;
	    
	    //change();
	    set(g2);
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		//repaint();
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		
		if (e.getKeyCode() == KeyEvent.VK_UP) {
			m.up();
		}
		else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
			m.down();
		}
		else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			m.right();
		}
		else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			m.left();
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
//		System.out.println("keyrelease");
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
//		System.out.println("keytyped");
	}

	
	private String getBuildName(int x, int y) {
		String name = "";
		
		for(int i=0; i<this.map_point.length; i++) {
			if (this.map_point[i][0] <= x && x <= this.map_point[i][2] && this.map_point[i][1] <= y && y <= this.map_point[i][3]) {
				name = this.m.getRoadMap(this.map_point[i][4], this.map_point[i][5]).getBuildName();
				break;
			}
		}
		
		return name;
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		int x = e.getPoint().x;
		int y = e.getPoint().y;
		String name = getBuildName(x, y);
		Building b = this.m.searchBuilding(name);
		
		String mode = this.m.getMode();
		
		if (b != null) {
			Main.Navi.con.removeAll();
			
			GUI_info info = new GUI_info(b);
			info.setBounds(0, 0, 800, 500);
			Main.Navi.con.add(info);
			
			Main.Navi.con.validate();
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
